/**
 * yyyy-mm-dd
 */
function isValidDate(strDate) {
     var datePat = /^(\d{4})(-)(\d{2})(-)(\d{2})$/;
     var matchArray = strDate.match(datePat);

     if (matchArray == null) return false;

     // matchArray[0] will be the original entire string, for example, 4-12-02 or 4/12/2002
     var year = matchArray[1];        // (\d{2}|\d{4}) - 5th parenthesis set - 02 or 2002
     var month = matchArray[3];     // (\d{1,2}) - 1st parenthesis set - 4
     var day = matchArray[5];         // (\d{1,2}) - 3rd parenthesis set - 12

     if (month < 1 || month > 12) return false;
     if (day < 1 || day > 31) return false;
     if ((month == 4 || month == 6 || month==9 || month == 11) && day == 31) return false;
     if (month == 2) {
          var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));

          if (day > 29 || (day == 29 && !isleap)) return false;
     }
     return true;
}

/**
 * Version: 0.1
 * Author: wujiyong
 * Date: 2008/03/10
 *
 */

if(Geni == undefined){
	var Geni = {};
}

Geni.Menu = {
	lastBarID:		null,
	lastGroupID:	null,
	ExpandMenu:			function(oBarID,oGroupID,b) {
		if ( this.lastBarID != null && this.lastBarID != oBarID ) {
			var oLastBar=document.getElementById(this.lastBarID);
			var oLastGroup=document.getElementById(this.lastGroupID);
			if (oLastBar.className=="g-leftmenu-expand") {
				oLastBar.className="g-leftmenu-collapse";
				oLastGroup.style.display="none";
			}
		}
		var oBar=document.getElementById(oBarID);
		var oGroup=document.getElementById(oGroupID);
		if (oBar.className=="g-leftmenu-collapse") {
			oBar.className="g-leftmenu-expand";
			oGroup.style.display="block";
		}
		else if (oBar.className=="g-leftmenu-expand") {
			oBar.className="g-leftmenu-collapse";
			oGroup.style.display="none";
		}
		if ( b == false ) {
			this.lastBarID=oBarID;
			this.lastGroupID=oGroupID;
		}
		return false;
	}
};

/**
 * Version: 0.1
 * Author: wujiyong
 * Date: 2008/03/10
 *
 */
if(Geni == undefined){
	var Geni = {};
}

Geni.Popup = {
	modalDialogID:		"window_modal_popup.ID",
	returnValue:		null,
	dialogArguments:	null,
	callBackFunction:	null,
	

	showModalDialog:			function(arguments) {
		return this.showWindow(arguments);
	},
	
	showWindow:			function(arguments) {
		this.callBackFunction=null;
		if ( arguments.returnCallback ) {
			this.callBackFunction=arguments.returnCallback;
		}
		if ( arguments.dialogArguments ) {
			this.dialogArguments=arguments.dialogArguments;
		}
	
		var options = Object.extend({
	      className:         "dialog",
	      minimizable:       false,
	      maximizable:       false,
	      resizable:         true,
	      closable:          true,
	      draggable:         true,
	      title:             "&nbsp;",
	      parent:            document.body,
	      url:               null,
	      width:             200,
	      height:            300,
	      closeCallback:     null,
	      modal:             true,
	      destroyOnClose:    true
		},arguments || {} );
	
		if ( !arguments.top ) {
			var top = (WindowUtilities.getPageSize().windowHeight-options.height)/2-20;
			options.top = top<0?10:top;
		}
		if ( !arguments.left ) {
			var left = (WindowUtilities.getPageSize().windowWidth-options.width)/2;
			options.left = left<0?10:left;
		}
	
		var win = Windows.getWindow(this.modalDialogID);
		if (win) {
			$(this.modalDialogID).remove();
			win = null;
		} 
		win = new Window(this.modalDialogID,options);
		win.show( options['modal'] );
	   
	    return false;
	},
	
	setDialogArguments:	function(s) {
		this.dialogArguments=s;
	},
	
	getDialogArguments:	function() {
		if ( parent ) {
			if ( parent.Geni.Popup.dialogArguments ) {
				return parent.Geni.Popup.dialogArguments;
			}
		}
		return null;
	},
	
	closeModalDialog:	function() {
		return this.closeWindow();
	},
	
	closeWindow:		function() {
		Windows.close(this.modalDialogID);
		return false;
	},
	
	close:				function(arguments) {
		this.returnValue=arguments;

		return this.closeSelfWindowCallBack();
	},
	
	closeSelfWindowCallBack:	function() {
		if ( parent ) {
			if ( parent.Geni.Popup.callBackFunction != null ) {
				parent.Geni.Popup.callBackFunction(this.returnValue);
			}
		}
		return this.closeSelfWindow();
	},
	
	closeSelfWindow:	function() {
		if ( parent ) {
			if ( parent.closeModalDialog ) {
				parent.closeModalDialog();
			}
		}
	
		return false;
	},
	
	textAreaPopup:		function(event,sourceId,options) {
		var element = event.currentTarget||window.event.srcElement;
		var positionOffset=Position.cumulativeOffset(element);
	
		var win = new Window(
			{
				className: "dialog",
				title: "TextArea...",
				maximizable: false,
				minimizable: false,
				resizable: false,
				closable: false,
				width:350,
				height:200,
				modal:true,
				top:positionOffset[1]+element.clientHeight,
				left:positionOffset[0]
			}
		);
	
		// window id
		var winID = win.getId();
	
		// Textarea
		var textArea = document.createElement('textarea');
		textArea.setAttribute("id",winID+"_textarea");
		textArea.setAttribute("rows","1");
		textArea.setAttribute("cols","1");
		textArea.setAttribute("class","popup-memo-box-text");
		// bug. \n -> <br>
		textArea.innerHTML=$(sourceId).value;
		textArea.value=$(sourceId).value;
	
		var divCenter = document.createElement('div');
		divCenter.setAttribute("align","center");
	
		// OK button
		var buttonOK=document.createElement("input");
		buttonOK.setAttribute("type","button");
		buttonOK.setAttribute("value","OK");
		buttonOK.setAttribute("onclick","document.getElementById(\"" + sourceId + "\").value=document.getElementById(\"" + winID + "_textarea\").value;Windows.close(\"" + winID + "\",event);");
		buttonOK.setAttribute("class","popup-memo-box-button");
		divCenter.appendChild(buttonOK);
	
		// Cancel button
		var buttonCancel=document.createElement("input");
		buttonCancel.setAttribute("type","button");
		buttonCancel.setAttribute("value","Cancel");
		buttonCancel.setAttribute("onclick","Windows.close(\"" + winID + "\",event);");
		buttonCancel.setAttribute("class","popup-memo-box-button");
		divCenter.appendChild(buttonCancel);
	
		var div = document.createElement('div');
		div.appendChild(textArea);
		div.appendChild(divCenter);
	
		win.getContent().innerHTML = div.innerHTML;
	
		win.options['onShow']=function(){document.getElementById(winID + "_textarea").focus();};
		win.show(true);
	
		return false;
	}
};

/**
 * Version: 0.1
 * Author: wujiyong
 * Date: 2008/03/10
 */
if(Geni == undefined){
	var Geni = {};
}
Geni.Scale = {
	setScalePercent:	function(divID, percent) {
		var dPercent=percent<0.00?0.00:percent>100.00?100.00:percent;
		
		if ( document.getElementById(divID) ) {
			var obj_b = document.getElementById(divID+"_b");
			var obj_f = document.getElementById(divID+"_f");
			if ( obj_b.className == "scale-negative-align" ) {
				obj_f.style.width=dPercent + "%";
			}
			else if ( obj_b.className == "scale-negative-vertical" ) {
				// IE bug by percent height
				var height = ( obj_b.clientHeight * dPercent ) / 100.00;
				obj_f.style.height = height + "px";
			}
			obj_f.title=percent + "%";
		}
	},
	setScaleColor:		function(divID, color) {
		if ( document.getElementById(divID) ) {
			var obj_f = document.getElementById(divID+"_f");
			obj_f.style.background=color;
		}
	}
};

/**
 * Version: 0.1
 * Author: wujiyong
 * Date: 2008/03/10
 *
 * status:  enable disable forbid
 */
if(Geni == undefined){
	var Geni = {};
}

// array button plugins     2008-03-26 by wujiyong
jQuerySetButtonStatus=function(id,status) {
	jQuery("[id=" + id + "]").each( function() {
		var obj=jQuery(this)[0];
		Geni.ToolBar.__setButtonStatus(obj,status);
	});
};

Geni.ToolBar = {
	idValue:						null,
	idName:							null,
	buttonOptions:					null,
	callBackOnClickFunction:		null,
	callBackOnDoubleClickFunction:	null,
	doubleClickActionId:			null,
	eLastRowSrc:					null,
	eLastRowClassName:				null,
	
	setId:		function(id) {
		this.idValue=id;
		if ( this.idName != null ) {
			this.$(this.idName).value=this.idValue;
		}
	},

	getId:		function() {
		if ( this.idName != null ) {
			return this.$(this.idName).value;
		}
		else {
			return this.idValue;
		}
	},

	$:		function(id) {
		return document.getElementById(id);
	},
	
	addButtonOptions:	function(source) {
		if ( this.buttonOptions == null ) {
			this.buttonOptions = source;
		}
		else {
			for (var property in source)
				this.buttonOptions[property] = source[property];
		}
	},

	setCallBackOnClick:	function(callback) {
		this.callBackOnClickFunction = callback;
	},

	setCallBackOnDoubleClick:	function(callback) {
		this.callBackOnDoubleClickFunction = callback;
	},

	setDoubleClickAction:	function(id) {
		this.doubleClickActionId=id;
	},

	setToolBarStatus:	function(options) {
		if ( !(options == null || options == "null" || options == "") ) {
			for(p in options) {
				var status=options[p];
				// this.setButtonStatus(p,status);
				jQuerySetButtonStatus(p,status);
			}
		}
	},
	
	setButtonStatus:	function(id,status) {
		var obj = this.$(id);
		this.__setButtonStatus(obj,status);
	},

	__setButtonStatus:	function(obj,status) {
		if ( status == "enabled" ) {
			obj.disabled=false;
			obj.style.display="inline";
		}
		else if ( status == "disabled" ) {
			obj.disabled=true;
			obj.style.display="inline";
		}
		else if ( status == "forbidden" ) {
			obj.disabled=true;
			obj.style.display="none";
		}
		else {
			obj.disabled=true;
			obj.style.display="inline";
		}
	},

	OnClick:	function(rowNum, selId, eRowSrc, buttonOperation) {
	    if ( eRowSrc.tagName == "TR" ) {
	    	// setId
	    	this.setId(selId);
	
	        if ( this.eLastRowSrc != eRowSrc ) {
	            if ( this.eLastRowSrc != null ) {
	                this.eLastRowSrc.className=this.eLastRowClassName;
	            }
	            this.eLastRowClassName = eRowSrc.className;
	            eRowSrc.className="selectClass";
	            this.eLastRowSrc = eRowSrc;
	        }
	        if ( buttonOperation == null || buttonOperation == "null" || buttonOperation == "" ) {
		        this.setToolBarStatus(this.buttonOptions);
	        }
	        else {
		        this.setToolBarStatus(buttonOperation);
	        }
	        // call back
	        if ( this.callBackOnClickFunction != null ) {
	            this.callBackOnClickFunction(selId,rowNum);
	        }
	    }
	    return false;
	},
	
	OnDblClick:	function(selId,rowNum) {
	    // call back
	    if ( this.callBackOnDoubleClickFunction != null ) {
	        this.callBackOnDoubleClickFunction(selId,rowNum);
	    }
	    if ( this.doubleClickActionId != null ) {
	        var obj = this.$(this.doubleClickActionId);
	        if ( obj.disabled == false ) {
	            obj.click();
	        }
	    }
	}
};

/**
 * Version: 0.1
 * Author: wujiyong
 * Date: 2008/03/10
 */
if(Geni == undefined){
	var Geni = {};
}

Geni.Tabs = {
	getNextSibling:		function(startBrother) {
		var endBrother=startBrother.nextSibling;
		while(endBrother.nodeType!=1){
			endBrother = endBrother.nextSibling;
		}
		return endBrother;
	},
	setTabPanel:		function(event) {
		var obj = (event.target)?event.target:window.event.srcElement;
	
		while (obj) {
			if ( obj.getAttribute("geniinsertionindex") != null ) {
				break;
			}
			obj = obj.parentNode;
		}
		var geniinsertionindex = obj.getAttribute("geniinsertionindex");
		if ( geniinsertionindex == null ) {
			return false;
		}
	
		var childonindex = 0;
		var obj1 = obj.parentNode;
		for ( var i = 0; i < obj1.childNodes.length; i++ ) {
			var childObj = obj1.childNodes[i];
			if ( childObj.nodeType == 1 ) {
				if ( geniinsertionindex == childonindex ) {
					childObj.className="geniTab current";
				}
				else {
					childObj.className="geniTab";
				}
				childonindex++;
			}
		}
	
		var childonindex = 0;
		var obj2 = this.getNextSibling(obj.parentNode);
		for ( var i = 0; i < obj2.childNodes.length; i++ ) {
			var childObj = obj2.childNodes[i];
			if ( childObj.nodeType == 1 ) {
				if ( geniinsertionindex == childonindex ) {
					childObj.style.display="block";
				}
				else {
					childObj.style.display="none";
				}
				childonindex++;
			}
		}
	}
};

/**
 * Version: 0.1
 * Author: wujiyong
 * Date: 2008/04/11
 *
 */
if(Geni == undefined){
	var Geni = {};
}

Geni.Table = {
	appendRow:		function(event,id,cleanValue,isAppendToEnd) {
		if(isAppendToEnd == undefined){
			isAppendToEnd = true;
		}
		
		var obj = (event.target)?event.target:window.event.srcElement;
		var objTable=document.getElementById(id);
		
		var objTbody=objTable.tBodies[0];
	
		//the model row index which will be cloned to create a new row
		var modelRowIndex = 0;
		if(isAppendToEnd){
			//last row index
			modelRowIndex = objTable.rows.length - 1;
			if(modelRowIndex < 0){
				modelRowIndex = 0;
			}
		}else{
			var tr = obj.parentNode.parentNode;
			modelRowIndex = tr.rowIndex;
		}
		if ( objTable ) {
			var len = objTable.rows.length;
			if ( len > 1 ) {
				// clone row
				var clone_node = (objTable.rows[modelRowIndex]).cloneNode(true);
				clone_node.className=clone_node.className=="odd"?"even":"odd";
				var childNodes=clone_node.childNodes;
				
				// set count
				var countTD = jQuery(clone_node).find("TD[class=count]")[0];
				var count = parseInt(countTD.innerHTML)+1;;
				var insertRow = objTable.insertRow(modelRowIndex + 1);
				var nTD;
				for(var i=0; i<clone_node.childNodes.length; i++){
					insertRow.appendChild(clone_node.childNodes[i].cloneNode(true));
				}
				insertRow.className = clone_node.className;
				
				count = (count>0?count-1:0);
				var tableRows = objTable.rows;
				for(var j=count; j<tableRows.length; j++){
					var elementType = {input:null,select:null,textarea:null,a:null,script:null};
					for (var element in elementType) {
						var childInputNodes = tableRows[j].getElementsByTagName(element);
						for ( var i = 0; i < childInputNodes.length; i++ ) {
							var e = childInputNodes[i];
							if(e.tagName == "SCRIPT"){
								var scriptContent = e.innerHTML;
								scriptContent = scriptContent.replace( /\[.*\]/g,"[" + (j - 1) + "]");
								scriptContent = scriptContent.replace( /_.*_/g,"_" + (j - 1) + "_");
								
								e.innerHTML = scriptContent;
								eval(scriptContent);
							}else{
								var name = e.name;
								name=name.replace( /\[.*\]/g,"[" + (j - 1) + "]");
								e.name=name;
								e.id = e.id.replace( /\[.*\]/g,"[" + (j - 1) + "]");
								e.id = e.id.replace( /_.*_/g,"_" + (j - 1) + "_");
								if ( cleanValue &&  j == (count + 1)) {
									if ( e.type=="hidden" || e.type=="text" ) {
										e.value="";
									}
									if(e.type == "checkbox"){
										e.checked = false;
									}else if(e.type == "radio"){
										e.checked = false;
									}else if(e.tagName == "TEXTAREA"){
										e.value = "";
									}
								}
							}
						} 
					}
					countTD = jQuery(tableRows[j]).find("TD[class=count]")[0];
					countTD.innerHTML = "" + j ;
				}
				
				
				var form = this._getFormAncestor(clone_node);
				if(form != null){
					form[clone_node.name] = clone_node;
				}
			}
		}
		return false;
	},
	_getFormAncestor: function(element){
		var result = null;
		var parentNode = element.parentNode;
		while(parentNode != null){
			if(parentNode.tagName == 'FORM'){
				result = parentNode;
				break;
			}
			parentNode = parentNode.parentNode;
		}
		return result;
	},
	getTable:		function(obj) {
		while(obj) {
			if ( obj.tagName == "TABLE" ) {
				break;
			}
			obj = obj.parentNode;
		}
		return obj;
	},
	
	removeRow:		function(event,id,isDeleteFromEnd) {
		if(isDeleteFromEnd == undefined){
			isDeleteFromEnd = true;
		}
		var obj = (event.target)?event.target:window.event.srcElement;
		var objTable=document.getElementById(id);
		var objTbody=objTable.tBodies[0];
		var deleteRowIndex = 0;
		if(isDeleteFromEnd){
			//last row index
			deleteRowIndex = objTable.rows.length - 1;
			if(deleteRowIndex < 0){
				deleteRowIndex = 0;
			}
		}else{
			var tr = obj.parentNode.parentNode;
			deleteRowIndex = tr.rowIndex;
		}
		if ( objTable ) {
			var len = objTable.rows.length;
			if ( len > 2 ) {
				objTable.deleteRow(deleteRowIndex);
				var tableRows = objTable.rows;
				for(var j=deleteRowIndex; j<tableRows.length; j++){
					var elementType = {input:null,select:null,textarea:null};
					for (var element in elementType) {
						var childInputNodes = tableRows[j].getElementsByTagName(element);
						for ( var i = 0; i < childInputNodes.length; i++ ) {
							var e = childInputNodes[i];
							var name = e.name;
							name=name.replace( /\[.*\]/g,"[" + (j - 1) + "]");
							e.name=name;
							//e.id = e.id.replace( /\[.*\]/g,"[" + (j - 1) + "]");
							//e.id = e.id.replace( /_.*_/g,"_" + (j - 1) + "_");
						} 
					}
					var countTD = jQuery(tableRows[j]).find("TD[class=count]")[0];
					countTD.innerHTML = "" + j ;
				}
			}else{
				//alert("There is only one row left! You can't delete the last row!");
			}
		}
		return false;
	}
};

/**
 * Version: 0.1
 * Author: wujiyong
 * Date: 2008/04/14
 *
 */

if(Geni == undefined){
	var Geni = {};
}

Geni.ExpandPanel = function(panel){
	if(isPanelExpanded(panel)){
		collapsePanel(panel);
	}else{
		expandPanel(panel);
	}

	function removeClass(obj,cl){
		if(obj&&obj.className){
			var rep=obj.className.match(' '+cl)?' '+cl:cl;
			obj.className=obj.className.replace(rep,'');
		}
	};

	function addClass(element,_class){
		if(!hasClass(element,_class)){
			element.className+=element.className?(" "+_class):_class;
		}
	};

	function hasClass(element,_className){
		if(!element){
			return;
		}
		var upperClass=_className.toUpperCase();
		if(element.className){
			var classes=element.className.split(' ');
			for(var i=0;i<classes.length;i++){
				if(classes[i].toUpperCase()==upperClass){
					return true;
				}
			}
		}
		return false;
	};

	function isPanelExpanded(panel){
		return hasClass(panel,'expanded');
	};

	function collapsePanel(panel){
		if(isPanelExpanded(panel)){
			removeClass(panel,'expanded');
		}
	};

	function expandPanel(panel){
		if(!isPanelExpanded(panel)){
			addClass(panel,'expanded');
		}
	};
};
